/**
 * Created by lx on 2018/4/3.
 */
CommonUtils.regNamespace("com.im.moneyPlaneManage.moneyFirstTrial");

$(function() {
    com.im.moneyPlaneManage.moneyFirstTrial.invoke.init();
});

com.im.moneyPlaneManage.moneyFirstTrial.invoke = function() {
    var client = new com.im.common.ajax();

    /**
     * 页面初始化
     * @private
     */
    function _init() {
        //初始化时间、复选框、下拉框等事件和样式
        _initCommpentAction();
        //资金计划列表查询
        $("#vQueryMoneys").on("click", function () {
            _queryMoneys(1);
        }).trigger("click");
        //条件重置
        $("#vResetCond").on("click", _resetCond);
        //注册资金审核
        $("#vUpdateMoney").on("click", _updateMoney);
        //失去聚焦
        $("#bu_first").keyup(function () {
            _initTotal();
        });
        $("#sheng_first").keyup(function () {
            _initTotal();
        });
        $("#zheng_first").keyup(function () {
            _initTotal();
        });
        $("#qi_first").keyup(function () {
            _initTotal();
        });
        $("#shi_first").keyup(function () {
            _initTotal();
        });
        $("#other_first").keyup(function () {
            _initTotal();
        });
    }
    function _initTotal() {
        var b = $("#bu_first").val();
        var s = $("#sheng_first").val();
        var g = $("#zheng_first").val();
        var q = $("#qi_first").val();
        var sh = $("#shi_first").val();
        var o= $("#other_first").val();

        var tmp = 0;
        if(b != ''){
            tmp = tmp + parseFloat(b);
        }
        if(s != ''){
            tmp = tmp + parseFloat(s);
        }
        if(g != ''){
            tmp = tmp + parseFloat(g);
        }
        if(q != ''){
            tmp = tmp + parseFloat(q);
        }
        if(sh != ''){
            tmp = tmp + parseFloat(sh);
        }
        if(o != ''){
            tmp = tmp + parseFloat(o);
        }
        $("#total_first").val(tmp.toFixed(2));
    }

    function _initCommpentAction() {
        //时间插件
        $('#startDatepicker,#endDatepicker').datepicker();
        //复选框
        $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
            checkboxClass: 'icheckbox_minimal-blue',
            radioClass   : 'iradio_minimal-blue'
        });
        //下拉框
        $('.select2').select2();

        //查询-建设单位to实施项目
        $("#searchOrgName").project2unit2itemCommpent({
            elmIdPlanTypeSel : "",
            elmIdUnitSel : "searchOrgName",//组织单位下拉元素
            elmIdItemSel : "searchImpleProjName",//实施项目下拉元素
            orgType : "10",//初始化单位时，单位类型
            callbackFunc: function (callBackType, ret) {//下拉框值改变时回调
            	_queryMoneys(1);
            }
        });
        $("#orgName").project2unit2itemCommpent({
            elmIdPlanTypeSel : "",
            elmIdUnitSel : "orgName",//组织单位下拉元素
            elmIdItemSel : "impleProjName",//实施项目下拉元素
            orgType : "10",//初始化单位时，单位类型
            callbackFunc: function (callBackType, ret) {//下拉框值改变时回调
            }
        });

        //查询投资计划批次
        var req = {};
        req.declareType = 20;
        req.hasYearDt = 'Y';
        var resp = client.callAsJson("investPlanService/queryDeclareSetIds", req);
        var fundBatch = $("#fundBatch");
        var fundBatch2 = $("#fundBatch2");
        $.each(resp.result, function (i, item) {
            if(i == 0) {
                fundBatch.append("<option value='" +  item.declareSetId  + "' year='" + item.yearDt + "' selected='selected'>" + item.declareCode + "</option>");
                fundBatch2.append("<option value='" +  item.declareSetId  + "' year='" + item.yearDt + "' selected='selected'>" + item.declareCode + "</option>");
            } else {
                fundBatch.append("<option value='" +  item.declareSetId  + "' year='" + item.yearDt + "'>" + item.declareCode + "</option>");
                fundBatch2.append("<option value='" +  item.declareSetId  + "' year='" + item.yearDt + "'>" + item.declareCode + "</option>");
            }
        });

        $("#fundBatch").on("change",function(){
            _queryMoneys(1);
        });
    }

    /**
     * 资金计划列表查询
     * @param page
     * @param needTurn
     * @private
     */
    function _queryMoneys(index) {
        var batchYear = $("#fundBatch option:selected").attr("year");

        var columns=[[
            {field:'全选',title:"全选",width:30,checkbox:true,rowspan:2,fitColumns:true},
            {field:'序号',title:"序号",width:$(this).width()*0.03,fitColumns:true,rowspan:2,
                formatter: function(value,row,index){
                    return  index+1;
                }
            },
            {field:'impleProjName',title:"项目名称",width:$(this).width()*0.1,fitColumns:true,rowspan:2,align:'center'},
            {field:'developOrgName',title:"建设单位",width:$(this).width()*0.1,fitColumns:true,rowspan:2,align:'center'},
            {title:"建设年限",colspan:2,fitColumns:true,align:'center'},
            {field:'preWorkSituation',title:"前期工作情况",width:$(this).width()*0.1,fitColumns:true,rowspan:2,align:'center'},
            {title:"概算总投资",colspan:7,fitColumns:true,align:'center'},
            {title:"累计至" + (batchYear) + "年下达的计划",colspan:7,fitColumns:true,align:'center'},
            {title:"累计至" + (batchYear) + "年完成的投资",colspan:7,fitColumns:true,align:'center'},
            {title:"累计至" + (batchYear) + "年到位的资金",colspan:7,fitColumns:true,align:'center'},
            {title: batchYear + "年填报投资计划",colspan:10,fitColumns:true,align:'center'},
            {title: batchYear + "年初审投资计划",colspan:8,fitColumns:true,align:'center'},
            {field:'operation',title:"操作",width:$(this).width()*0.06,rowspan:2,
                formatter: function(value,row,index){
                    return  '<a href="javascript:; "onclick="com.im.moneyPlaneManage.moneyFirstTrial.invoke.showUpdateMoney(\''+index+'\')" title="审核"><i class="fa fa-pencil"></i></a> ';
                }
            }],
            [	{field:'investPlanStartDt',title:"开工年",fitColumns:true},
                {field:'investPlanEndDt',title:"完工年",fitColumns:true},
                {field:'total_plan',title:"合计",fitColumns:true},
                {field:'bu_plan',title:"部补助",fitColumns:true},
                {field:'sheng_plan',title:"省财政",fitColumns:true},
                {field:'zheng_plan',title:"政府融资",fitColumns:true},
                {field:'shi_plan',title:"市州配套",fitColumns:true},
                {field:'qi_plan',title:"企业自筹",fitColumns:true},
                {field:'other_plan',title:"其它",fitColumns:true},
                {field:'total_impl',title:"合计",fitColumns:true},
                {field:'bu_impl',title:"部补助",fitColumns:true},
                {field:'sheng_impl',title:"省财政",fitColumns:true},
                {field:'zheng_impl',title:"政府融资",fitColumns:true},
                {field:'shi_impl',title:"市州配套",fitColumns:true},
                {field:'qi_impl',title:"企业自筹",fitColumns:true},
                {field:'other_impl',title:"其它",fitColumns:true},
                {field:'total_invest',title:"合计",fitColumns:true},
                {field:'bu_invest',title:"部补助",fitColumns:true},
                {field:'sheng_invest',title:"省财政",fitColumns:true},
                {field:'zheng_invest',title:"政府融资",fitColumns:true},
                {field:'shi_invest',title:"市州配套",fitColumns:true},
                {field:'qi_invest',title:"企业自筹",fitColumns:true},
                {field:'other_invest',title:"其它",fitColumns:true},
                {field:'total_fund',title:"合计",fitColumns:true},
                {field:'bu_fund',title:"部补助",fitColumns:true},
                {field:'sheng_fund',title:"省财政",fitColumns:true},
                {field:'zheng_fund',title:"政府融资",fitColumns:true},
                {field:'shi_fund',title:"市州配套",fitColumns:true},
                {field:'qi_fund',title:"企业自筹",fitColumns:true},
                {field:'other_fund',title:"其它",fitColumns:true},
                {field:'total_write',title:"合计",fitColumns:true},
                {field:'bu_write',title:"部补助",fitColumns:true},
                {field:'sheng_write',title:"省财政",fitColumns:true},
                {field:'zheng_write',title:"政府融资",fitColumns:true},
                {field:'shi_write',title:"市州配套",fitColumns:true},
                {field:'qi_write',title:"企业自筹",fitColumns:true},
                {field:'other_write',title:"其它",fitColumns:true},
                {field:'statusName',title:"状态",fitColumns:true},
                {field:'description',title:"备注",fitColumns:true},
                {field:'files',title:"填报附件",fitColumns:true,formatter: function(value,row,index){
                    if(value.length > 0){
                        var fileNames = "";
                        $.each(value, function (i, item) {
                            fileNames += item.attachFullName + "  ";
                        });
                        return fileNames;
                    }else {
                        return "";
                    }
                }},
                {field:'total_first',title:"合计",fitColumns:true},
                {field:'bu_first',title:"部补助",fitColumns:true},
                {field:'sheng_first',title:"省财政",fitColumns:true},
                {field:'zheng_first',title:"政府融资",fitColumns:true},
                {field:'shi_first',title:"市州配套",fitColumns:true},
                {field:'qi_first',title:"企业自筹",fitColumns:true},
                {field:'other_first',title:"其它",fitColumns:true},
                {field:'examineDescription',title:"审核描述",fitColumns:true}
            ]
        ];

        var req = {};
        req.pageInfo = {};
        req.pageInfo.pageNumber = index;
        req.pageInfo.pageSize = 20;
        req.impleProjId = $("#searchImpleProjName option:selected").attr("value");;
        req.developOrg = $("#searchOrgName option:selected").attr("value");
        req.declareSetId = $("#fundBatch option:selected").attr("declareSetId");

        var resp = client.callAsJson("fundPlanService/queryAuditFundPlanList", req);
        if (!resp.success) {
            toastr.error(resp.resultMsg);
            return;
        }

        $.each(resp.result.rows, function (i, one) {
            $.each(one.fundDetailVo, function (j, two) {
                if(two.type == "planProjMoney") {
                    one.bu_plan = two.fundBu;
                    one.sheng_plan = two.fundSheng;
                    one.zheng_plan = two.fundGao;
                    one.qi_plan = two.fundQi;
                    one.shi_plan = two.fundShi;
                    one.other_plan = two.fundOther;
                    one.total_plan = two.total;
                } else if(two.type == "implProjMoney") {
                    one.bu_impl = two.fundBu;
                    one.sheng_impl = two.fundSheng;
                    one.zheng_impl = two.fundGao;
                    one.qi_impl = two.fundQi;
                    one.shi_impl = two.fundShi;
                    one.other_impl = two.fundOther;
                    one.total_impl = two.total;
                } else if(two.type == "investPlanMoney") {
                    one.bu_invest = two.fundBu;
                    one.sheng_invest = two.fundSheng;
                    one.zheng_invest = two.fundGao;
                    one.qi_invest = two.fundQi;
                    one.shi_invest = two.fundShi;
                    one.other_invest = two.fundOther;
                    one.total_invest = two.total;
                } else if(two.type == "fundPlanMoney") {
                    one.bu_fund = two.fundBu;
                    one.sheng_fund = two.fundSheng;
                    one.zheng_fund = two.fundGao;
                    one.qi_fund = two.fundQi;
                    one.shi_fund = two.fundShi;
                    one.other_fund = two.fundOther;
                    one.total_fund = two.total;
                } else if(two.type == "fundWriteMoney") {
                    one.bu_write = two.fundBu;
                    one.sheng_write = two.fundSheng;
                    one.zheng_write = two.fundGao;
                    one.qi_write = two.fundQi;
                    one.shi_write = two.fundShi;
                    one.other_write = two.fundOther;
                    one.total_write = two.total;
                } else if(two.type == "fundFirstMoney") {
                    one.bu_first = two.fundBu;
                    one.sheng_first = two.fundSheng;
                    one.zheng_first = two.fundGao;
                    one.qi_first = two.fundQi;
                    one.shi_first = two.fundShi;
                    one.other_first = two.fundOther;
                    one.total_first = two.total;
                }
            });
        });

        $('#vMoneyTable').datagrid({
            pagination:true,
            nowrap:false,
            fitColumns:true,
            columns:columns,
            data:resp.result,
        });
        var p = $('#vMoneyTable').datagrid('getPager');
        $(p).pagination({
            pageSize: 20,
            pageList: [10,20,30],
            beforePageText: '第',
            afterPageText: '页    共 {pages}  页',
            displayMsg: '当前显示 {from} - {to} 条记录 共 {total}  条记录',
            onSelectPage:function(pageNumber, pageSize){
                _initDataList(pageNumber);
            }
        });

    }
    
    //批量审核
    $('#batchTrial').click(function (){
        if($('#vMoneyTable').datagrid('getChecked').length == 0) {
            com.im.common.confirm("请选择项目", null, function () {
                return;
            });
        } else {
            $("#batchEditModal").modal("show");
        }
    });
    $("#saveBtn2").click(function(){
    	_batchFirstTrial();
    });
    $("#saveBtn").click(function(){
    	_updateMoney();
    });
    function _batchFirstTrial(){
        var fundPlanIds = [];
        $.each($('#vMoneyTable').datagrid('getChecked'), function(index, item){
        	fundPlanIds[index] = item.fundPlanId;;
    	});

        var saveparam={};
        saveparam.fundPlanIds = fundPlanIds;
        saveparam.operType="firstAudit";
        saveparam.examineDescription = com.im.common.util.escapeTextArea($("#examineDescription2").val());
        saveparam.statusCd=$("#examineResult2").val();

        var result = client.callAsJson("fundPlanService/batchAuditFundPlan", saveparam);
        if(result.resultCode=='0'){
            toastr.success("保存成功！");
            _queryMoneys(1);
            $("#batchEditModal").modal("hide");
        }else {
            toastr.error("服务器忙！请稍后再试！"+result.resultMsg);
        }
    }
    /**
     * 资金计划审核
     * @param index
     * @private
     */
    function _showUpdateMoney(index) {
        var data = $('#vMoneyTable').datagrid('getData').rows[index];
        $("#fundPlanId").val(data.fundPlanId);
        //$("#fundBatch2").find("option[value='" + data.fundBatch2 + "']").attr("selected",true);
        $("#orgName").val(data.orgId).trigger("change");
        $("#impleProjName").val(data.impleProjId).trigger("change");
        $("#bu_plan").val(data.bu_plan);
        $("#sheng_plan").val(data.sheng_plan);
        $("#zheng_plan").val(data.zheng_plan);
        $("#qi_plan").val(data.qi_plan);
        $("#shi_plan").val(data.shi_plan);
        $("#other_plan").val(data.other_plan);
        $("#total_plan").val(data.total_plan);
        $("#bu_impl").val(data.bu_impl);
        $("#sheng_impl").val(data.sheng_impl);
        $("#zheng_impl").val(data.zheng_impl);
        $("#qi_impl").val(data.qi_impl);
        $("#shi_impl").val(data.shi_impl);
        $("#other_impl").val(data.other_impl);
        $("#total_impl").val(data.total_impl);
        $("#bu_invest").val(data.bu_invest);
        $("#sheng_invest").val(data.sheng_invest);
        $("#zheng_invest").val(data.zheng_invest);
        $("#qi_invest").val(data.qi_invest);
        $("#shi_invest").val(data.shi_invest);
        $("#other_invest").val(data.other_invest);
        $("#total_invest").val(data.total_invest);
        $("#bu_fund").val(data.bu_fund);
        $("#sheng_fund").val(data.sheng_fund);
        $("#zheng_fund").val(data.zheng_fund);
        $("#qi_fund").val(data.qi_fund);
        $("#shi_fund").val(data.shi_fund);
        $("#other_fund").val(data.other_fund);
        $("#total_fund").val(data.total_fund);
        $("#bu_write").val(data.bu_write);
        $("#sheng_write").val(data.sheng_write);
        $("#zheng_write").val(data.zheng_write);
        $("#qi_write").val(data.qi_write);
        $("#shi_write").val(data.shi_write);
        $("#other_write").val(data.other_write);
        $("#total_write").val(data.total_write);
        $("#bu_first").val(data.bu_write);
        $("#sheng_first").val(data.sheng_write);
        $("#zheng_first").val(data.zheng_write);
        $("#qi_first").val(data.qi_write);
        $("#shi_first").val(data.shi_write);
        $("#other_first").val(data.other_write);
        $("#total_first").val(data.total_write);
        $("#addEditModal").modal("show");
    }

    /**
     * 资金计划审核
     * @private
     */
    function _updateMoney() {
        var req = {};
        req.fundPlanId = $("#fundPlanId").val();
        req.examineDescription = com.im.common.util.escapeTextArea($("#examineDescription").val());
        req.statusCd = $("#examineResult option:selected").val();
        req.operType = 'firstAudit';//初审
        req.fundSourceBu = $("#bu_first").val();
        req.fundSourceSheng = $("#sheng_first").val();
        req.fundSourceGao = $("#zheng_first").val();
        req.fundSourceQi = $("#qi_first").val();
        req.fundSourceShi = $("#shi_first").val();
        req.fundSourceOther = $("#shi_other").val();
        
        var resp = client.callAsJson("fundPlanService/auditFundPlan", req);
        if (!resp.success) {
            toastr.error(resp.resultMsg);
            return;
        }

        toastr.success("资金初审成功");
        _queryMoneys(1);
        $("#addEditModal").modal("hide");
    }

    /**
     * 条件重置
     * @private
     */
    function _resetCond() {
        $('#searchOrgName,#searchImpleProjName,#fundBatch').val('').trigger("change");
        _queryMoneys(1);
    }

    return {
        init : _init,
        showUpdateMoney : _showUpdateMoney
    }
}();